import {_decorator, Button, Component, director, Label, Node} from 'cc';
import {lobby} from "db://assets/script/lobby";
import {ZoneMapPlane} from "db://assets/script/game/mainui/battle/zone-map-plane";
import {GameEvent} from "db://assets/script/game-event";
import Long from "long";
import {userInfo} from "db://assets/script/user-info";
import {configsHelper} from "db://assets/script/configs-helper";
import {FishMoveCompleteReply} from "db://assets/proto/new-world-ws/v1/internal";
import {FishBeginMoveEvent} from "db://assets/script/common/events/fish-events/fish-begin-move-event";
import LabelTime from "db://assets/script/common/label-time";
import CommonRanks from "db://assets/script/game/mainui/ranks/common-ranks";
import {RankType} from "db://assets/proto/new-world-api/v1/user";
import {UiShowPoolRankEvent} from "db://assets/script/common/events/ui-events/ui-show-pool-rank-event";
import {MessageBox} from "db://assets/script/game/mainui/city/message-box";
import {FishCoreChangeEvent} from "db://assets/script/common/events/fish-events/fish-core-change-event";

const {ccclass, property} = _decorator;

@ccclass('UiInBattle')
export class UiInBattle extends Component {

    @property(Label)
    zoneName!: Label

    @property(LabelTime)
    moveLabel!: LabelTime

    @property(LabelTime)
    aliveDurationLabel!: LabelTime

    @property(Node)
    aliveDurationBox!: Node

    @property(Button)
    zoneMapBtn!: Button

    @property(Button)
    poolRankBtn!: Button

    @property(MessageBox)
    messageBox!: MessageBox


    start() {
    }

    onLoad() {
        this.moveLabel.prefixText = '赶路中 '
        this.aliveDurationLabel.prefixText = '探索: '
        this.aliveDurationBox.active = false
        this.onEvent()
    }

    onEvent() {
        this.zoneMapBtn.node.on(Button.EventType.CLICK, () => {
            const zoneMapPlaneNode = lobby.instance.addUIWithClickMask('prefab/common/zone-map-plane', 0, (node) => node.destroy())
            zoneMapPlaneNode.getComponent(ZoneMapPlane)!.init(null)
        })
        this.poolRankBtn.node.on(Button.EventType.CLICK, () => {
            const cff = userInfo.fishInfo.loadCurrentFish()
            let zoneId = Long.fromNumber(10001)
            if (cff) zoneId = cff.zoneId
            const poolRankNode = lobby.instance.addUIWithClickMask('prefab/ranks/common-ranks', 0, (node) => node.destroy())
            const poolRank = poolRankNode.getComponent(CommonRanks)!
            poolRank.show()
            poolRank.loadRank(RankType.level, zoneId)
        })
        director.on(GameEvent.uiChangeZoneName, this._changeZone, this)
        director.on(GameEvent.uiChangeAliveDuration, this._changeAliveDuration, this)
        director.on(GameEvent.fishMoveComplete, this.moveComplete, this)
        director.on(GameEvent.fishBeginMove, this.beginMove, this)
    }

    offEvent() {
        director.off(GameEvent.uiChangeZoneName, this._changeZone, this)
        director.off(GameEvent.uiChangeAliveDuration, this._changeAliveDuration, this)
        director.off(GameEvent.fishMoveComplete, this.moveComplete, this)
        director.off(GameEvent.fishBeginMove, this.beginMove, this)
    }

    private _changeZone(zoneId: Long) {
        const zoneConfig = configsHelper.loadZoneConfig(zoneId)
        if (zoneConfig) {
            this.zoneName.string = zoneConfig.zoneName
            lobby.instance.showToast('「' + zoneConfig.zoneName + '」')
            this.moveLabel.node.active = false
            this.zoneName.node.active = true
            this.moveLabel.reset()
        }
    }

    private _changeAliveDuration(goHomeAtMs: Long) {
        this.aliveDurationLabel.reset()
        if (goHomeAtMs.gt(0)) {
            this.aliveDurationBox.active = true
            this.aliveDurationLabel.setTimeByServerTime(goHomeAtMs.toNumber(), true)
            this.aliveDurationLabel.onComplete = () => {
                this.aliveDurationBox.active = false
                lobby.instance.showToast('探索完毕! 返回宗门')
            }
        } else {
            this.aliveDurationBox.active = false
        }
    }

    moveComplete(event: FishMoveCompleteReply) {
        if (!userInfo.fishInfo.currentShowFishInBattle || event.fishId.neq(userInfo.fishInfo.currentShowFishInBattle)) return
        this._changeZone(event.zoneId)
    }

    beginMove(event: FishBeginMoveEvent) {
        if (userInfo.fishInfo.currentShowFishInBattle && userInfo.fishInfo.currentShowFishInBattle.eq(event.fishId)) {
            this.moveLabel.setTimeByServerTime(event.completeAtMs.toNumber(), true)
            this.zoneName.node.active = false
            this.moveLabel.node.active = true
            this.moveLabel.onComplete = () => {
                this.moveLabel.node.active = false
                this.zoneName.node.active = true
                this.moveLabel.reset()
            }
        }
    }

    protected onDestroy(): void {
        this.offEvent()
    }


}